﻿using System;
using System.Collections.Generic;
using Krile.Kernel;
using Krile.Kernel.Control;
using Krile.Kernel.Data;
using Krile.Kernel.Plugin;
using Krile.Kernel.Plugin.Operations;
using Krile.Kernel.Plugin.Operations.AgentElements;

namespace Wassrile.Operation
{
    public class Operation : Krile.Kernel.Plugin.Operation, IAgent
    {
        IPlugin plugin = null;
        readonly Service provideService;
        readonly StatusControl statusControl;

        public Operation(IPlugin parent)
        {
            plugin = parent;
            provideService = new DataClasses.WassrService();
            statusControl = new StatusControl();
            Bridge.StatusManager.RegistNewSearchData(typeof(Searchs.SearchWassrStatus));
            Bridge.StatusManager.RegistNewSearchData(typeof(Searchs.SearchByChannel));
            Bridge.StatusManager.RegistNewSearchData(typeof(Searchs.SearchByChannelId));
            Bridge.StatusManager.RegistNewSearchData(typeof(Searchs.SearchFavCount));
        }

        public override void Begin()
        {
            //Initialie
            Core.InitCore(plugin as Plugin);
            Core.Config = Config.Config.Restore();
            //Set main datas
            Bridge.ControlManager.RegistInputMethod(new Controls.WInput());

            //Bridge.MainForm.AddCustomContextMenuCandidate();

            if (Core.Config.FirstBoot)
            {
                using (var ac = new Dialogs.AccountReq())
                {
                    switch (ac.ShowDialog())
                    {
                        case System.Windows.Forms.DialogResult.OK:
                            Core.Config.account.UserName = ac.UserId;
                            Core.Config.account.CredentialType = Wassrile.Config.Account.CredentialTypes.Token;
                            if (!ac.UseToken) Core.Config.account.CredentialType = Wassrile.Config.Account.CredentialTypes.BASIC;
                            Core.Config.account.PasswordOrToken = ac.Password;
                            break;
                        case System.Windows.Forms.DialogResult.Cancel:
                            Bridge.PluginManager.InactivatePlugin(plugin);
                            return;
                        default:
                            break;
                    }
                }
            }

            Action a = new Action(statusControl.Receiver.InitialReceive);
            a.BeginInvoke(new AsyncCallback((r) => { ((Action)r.AsyncState).EndInvoke(r); }), a);
        }

        public override void Exit()
        {
            if (Core.Config != null)
                Core.Config.Save();
        }

        public override Dictionary<string, ConfigPage> GetConfigPages()
        {
            var dic = new Dictionary<string, ConfigPage>();
            dic.Add("アカウントと受信", new Config.ConfigPage.AccountRecv());
            dic.Add("投稿と動作", new Config.ConfigPage.PostBehavior());
            dic.Add("詳細", new Config.ConfigPage.Detail());
            return dic;
        }

        public override Krile.Kernel.Plugin.Operation.OperationTypes OperationType
        {
            get { return OperationTypes.Agent; }
        }

        public override string ParentPluginId
        {
            get { return Core.Plugin.Id; }
        }

        public Service HostingService
        {
            get { return provideService; }
        }

        public string InputMethodId
        {
            get { return Define.InputMethodId; }
        }

        public IStatusControl Status
        {
            get { return statusControl; }
        }

        public bool IsMasterId(string userid)
        {
            return userid == Core.Config.account.UserName;
        }
    }
}
